export default {
  methods: {
    /**
     * 获取配置信息
     */
    async getAppConfig({ source }) {
      const res = await this.$get("/monitor/getConfig.json");
      if (res && Array.isArray(res.body)) {
        const configInfo = {};
        res.body.forEach((item) => {
          configInfo[item.key] = item.value;
        });
        this.setGlobalData("config", configInfo, source);
      }
      return res;
    },
    /**
     * 存储数据到globalData
     */
    setGlobalData(key, value, source) {
      if (source === "App") {
        this.globalData[key] = value;
      } else {
        getApp().globalData[key] = value;
      }
    },
    /**
     * 读取globalData数据
     */
    getGlobalData(key, source) {
      return source === "App" ? this.globalData[key] : getApp().globalData[key];
    },
    /**
     * 获取权限
     */
    async getPrimission() {
      const res = await this.$post("/admin/inner/21", {});
      if (res && res.body) {
        if (Array.isArray(res.body.permissionList)) {
          const permissionList = res.body.permissionList;
          uni.setStorage({
            key: "yf_primission",
            data: JSON.stringify(permissionList),
          });
          // 监控视频权限
          const video = "monitor:envmonitor:video";
          const videojt = "monitor:envmonitormine:video";
          // 采掘通权限
          const cjtjt = 'excavate_superior_index:index'
          const cjt = 'excavate_index:index'
           // 从业人员权限
          const cyt = 'cyt_index:index'
          // 公告通知
          const ggtz = "monitor:noticeAnnouncement:index";
          const hasGuardList = [
            "guard_home:index",
            "guard_en_home:index",
            "gsms:govreceiver:index",
            "guard_config:index",
            "gsms:smstemplate:index",
          ];
          let hasGuard = false;
          for (let i = 0; i < hasGuardList.length; i++) {
            let guard = hasGuardList[i];
            if (permissionList.includes(guard)) {
              hasGuard = true;
            }
          }
          this.setGlobalData("isGuard", hasGuard);
          this.setGlobalData("cjtAuth", permissionList.includes(cjt) || permissionList.includes(cjtjt));
          this.setGlobalData("ggtzAuth", permissionList.includes(ggtz));

          this.setGlobalData("videoAuth", permissionList.includes(video) || permissionList.includes(videojt));
          this.setGlobalData("isCyry", permissionList.includes(cyt));
        }
        this.setGlobalData("userInfoData", res.body.user);
      } else {
        this.setGlobalData("isGuard", false);
        this.setGlobalData("videoAuth", false);
        this.setGlobalData("isCyry", false);
      }
    },
  },
};
